package com.kekezu.kppw.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;
import com.kekezu.kppw.utils.StrFormat;

public class GoodData {
	/**
	 * 首页服务商
	 * 
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getIndexGood(
			Context context) {
		ArrayList<HashMap<String, Object>> aliList = new ArrayList<HashMap<String, Object>>();

		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/hotService";
		HttpUtils httpUtils = HttpUtils.getInstance();

		try {
			String strJson = httpUtils.httpGet(urlString, context);
			// Log.e("strJson", strJson);

			JSONObject jsonObject = new JSONObject(strJson);

			String resString = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONArray jsonArray = jsonObject.getJSONArray("data");

				for (int i = 0; i < jsonArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject itemJsonObject = jsonArray.optJSONObject(i);

					map.put("id", itemJsonObject.getString("id"));
					map.put("recommend_id",
							itemJsonObject.getString("recommend_id"));
					map.put("recommend_name",
							itemJsonObject.getString("recommend_name"));
					map.put("recommend_pic",
							itemJsonObject.getString("recommend_pic"));
					map.put("percent", itemJsonObject.getString("percent"));

					JSONArray jsontags = itemJsonObject.getJSONArray("tags");

					if (jsontags.length() <= 0) {
						map.put("tags", "[]");
					} else {
						JSONArray jjArray = new JSONArray();
						for (int j = 0; j < jsontags.length(); j++) {
							JSONObject itemJsonObject2 = jsontags
									.optJSONObject(j);
							jjArray.put(itemJsonObject2.getString("tag_name")
									.toString());
						}

						Log.e("jjArray", "" + jjArray);

						map.put("tags", jjArray);
					}

					// Log.e("pic", "" +
					// itemJsonObject.getString("recommend_pic"));

					aliList.add(map);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return aliList;
	}

	// 服务商列表
	public static ArrayList<HashMap<String, Object>> getGoodList(int page,
			int type, int category, String strName, Context context) {
		ArrayList<HashMap<String, Object>> aliList = new ArrayList<HashMap<String, Object>>();

		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/serviceList?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();

		params.put("page", String.valueOf(page));
		if (type != 0) {
			params.put("type", String.valueOf(type));
		} else if (category != 0) {
			params.put("category", String.valueOf(category));
		} else if (StrFormat.formatStr(strName)) {
			params.put("name", strName);
		}

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);

			JSONObject jsonObject = new JSONObject(strJson);
			String resString = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				JSONArray jsonArray = jsondata.getJSONArray("data");

				for (int i = 0; i < jsonArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject itemJsonObject = jsonArray.optJSONObject(i);

					map.put("recommend_id", itemJsonObject.getString("id"));
					map.put("recommend_name", itemJsonObject.getString("name"));
					map.put("recommend_pic", itemJsonObject.getString("avatar"));
					map.put("percent", itemJsonObject.getString("percent"));
					map.put("tags", itemJsonObject.getString("tags"));

					Log.e("tags", itemJsonObject.getString("tags"));

					aliList.add(map);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return aliList;
	}

}
